<?php

class Doctrine_Model_CodigoDao extends Doctrine_Model_BaseDao {

    public function obtenerTodos() {
        return $this->getEntityManager()
                ->createQuery('select c from Doctrine_Model_Entities_Codigo c')
                ->getResult();
    }

    public function obtenerPorId($id) {
        return $this->getEntityManager()->find('Doctrine_Model_Entities_Codigo', $id);
    }
    
    public function obtenerPorCodigo($codigo) {
        if (is_numeric($codigo)) {
            $result = $this->getEntityManager()
                    ->createQuery("SELECT c FROM Doctrine_Model_Entities_Codigo c WHERE c.codigo = {$codigo} AND c.fechaUso IS NULL ")
                    ->getResult();
            return $result[0];
        }
        return false;
    }

    public function guardar(Doctrine_Model_Entities_Codigo $codigo) {
        try {        
            $this->getEntityManager()->persist($codigo);
            $this->getEntityManager()->flush();
            return true;
        } catch (Exception $e) {
            $logger = Zend_Registry::get('logger');
            $logger->log("Error Codigo: " . $e->getMessage(), Zend_Log::ERR);
            return false;
        }
    }

    public function eliminar(Doctrine_Model_Entities_Codigo $codigo) {
        try {
            $this->getEntityManager()->remove($codigo);
            $this->getEntityManager()->flush();
            return true;
        } catch (Exception $e) {
            $logger = Zend_Registry::get('logger');
            $logger->log("Error Codigo: " . $e->getMessage(), Zend_Log::ERR);
            return false;
        }
    }

    public function toArrayCollection($coleccion) {
        $arrayCollection = new \Doctrine\Common\Collections\ArrayCollection();
        if (is_array($coleccion)) {
            foreach ($coleccion as $objeto) {
                $arrayCollection->add($objeto);
            }
        } else {
            if ($coleccion != NULL) {
                $arrayCollection->add($coleccion);
            }
        }
        return $arrayCollection;
    }

}

